package Connector
{
    
    import Libs.Global;
    
    import com.aurora.as3.client.SgsEvent;
    import com.aurora.as3.client.SimpleClient;
    import com.hexagonstar.util.debug.Debug;
    
    import flash.geom.*;
    import flash.utils.*;

    public class Connection extends Object
    {
        private var client:SimpleClient;
        private var curstate:String;
		private var clientName:String;

        public function Connection(cName:String) : void
        {
            this.curstate = "login";
			clientName  = cName;
            var _loc_1:* = new ByteArray();
            _loc_1.writeShort(-1);
            _loc_1.position = 0;
            return;
        }// end function

        public function onLoginSuccess(event:SgsEvent) : void
        {
            trace("onLoginSuccess");
            this.curstate = "online";
            this.send("Login " + clientName);
            return;
        }// end function

        public function onLoginFailure(event:SgsEvent) : void
        {
            trace("onLoginFailure:" + event.failureMessage);
            return;
        }// end function

        public function onLoginRedirect(event:SgsEvent) : void
        {
            trace("onLoginRedirect:" + event.host + " :" + event.port);
            return;
        }// end function

        public function onSessionMessage(event:SgsEvent) : void
        {
            var _loc_2:* = event.sessionMessage;
            var _loc_3:* = _loc_2.readUTFBytes(_loc_2.bytesAvailable);
            trace("onSessionMessage: [" + _loc_3 + "]");
            this.parseMessage(_loc_3);
            return;
        }// end function

        public function onChannelMessage(event:SgsEvent) : void
        {
            var _loc_2:* = event.channelMessage;
            var _loc_3:* = _loc_2.readUTFBytes(_loc_2.bytesAvailable);
            trace("onChannelMessage: [" + _loc_3 + "]");
            this.parseMessage(_loc_3);
            return;
        }// end function

        private function parseMessage(msg:String) : void
        {
            var _loc_2:Array = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:Number = NaN;
            var _loc_6:String = null;

            var _loc_8:String = null;
            var _loc_9:Number = NaN;
            var _loc_10:Number = NaN;
            _loc_2 = msg.split(" ");
            Debug.trace(msg);
            switch(_loc_2[0])
            {
				case "Login":
				{
					break;
				}
                case "JoinGame":
                {
                    break;
                }
                case "LeaveGame":
                {
                    break;
                }
                case "Move":
                {
                    break;
                }
                case "Catch":
                {
                    break;
                }
                case "Speed":
                {
                    break;
                }
                case "GenMoney":
                {
                    break;
                }
                case "GetMoney":
                {
                    break;
                }
                case "RemoveMoney":
                {
                    break;
                }
                case "GenTreasure":
                {
                    break;
                }
                case "RemoveTreasure":
                {
                    break;
                }
                case "SendMessage":
                {
                    break;
                }
                default:
                {
                    break;
                }
            }
        }// end function

        public function onLogout(event:SgsEvent) : void
        {
            trace("onLogout()");
            this.curstate = "login";
            return;
        }// end function

        public function isLogin() : Boolean
        {
            if (this.curstate == "online")
            {
                return true;
            }
            return false;
        }// end function

        public function login(username:String, passwd:String) : void
        {
            this.client = new SimpleClient(Global.CONN_HOST, Global.CONN_PORT);
            this.client.addEventListener(SgsEvent.LOGIN_SUCCESS, this.onLoginSuccess);
            this.client.addEventListener(SgsEvent.LOGIN_FAILURE, this.onLoginFailure);
            this.client.addEventListener(SgsEvent.LOGIN_REDIRECT, this.onLoginRedirect);
            this.client.addEventListener(SgsEvent.SESSION_MESSAGE, this.onSessionMessage);
            this.client.addEventListener(SgsEvent.CHANNEL_MESSAGE, this.onChannelMessage);
            this.client.addEventListener(SgsEvent.LOGOUT, this.onLogout);
            this.client.login(username, passwd);
            return;
        }// end function

        public function send(str:String) : Boolean
        {
            var _loc_2:ByteArray = null;
            if (this.curstate == "online")
            {
                _loc_2 = new ByteArray();
                if (str == "/quit")
                {
                    this.client.logout();
                }
                else
                {
                    _loc_2.writeUTFBytes(str);
                    this.client.sessionSend(_loc_2);
                }
                return true;
            }
            else
            {
                return false;
            }
        }// end function

    }
}
